declare module 'vue-router' {
    interface RouteMeta {
        title?: string
    }
}
import {createRouter, createWebHistory} from 'vue-router'
import {ElMessage} from "element-plus";
// @ts-ignore
// @ts-ignore
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        { path: '/', redirect: '/showshouye' },
        { path: '/sjcaipin', name: 'sjcaipin', meta: { title: '商家菜品' }, component: () => import('./pages/usershouye/ShangJiaCaiPin.vue')},
        { path: '/showshouye', name: 'showshouye', meta: { title: '首页', public:true }, component: () => import('./pages/usershouye/ShowShouye.vue')},
        { path:'/home', component: () => import('./pages/shenfen/ShowShenFen.vue')},
        { path:'/test', component: () => import('./pages/test/XiaDan.vue')},
        { path: '/home', component: () => import('./components/Home.vue'), children: [
            { path: 'qishou', name: 'qishou', meta: { title: '骑手信息' }, component: () => import('./pages/qishou/ShowQishou.vue') },
            { path: 'shoucang', name: 'shoucang', meta: {title: '收藏信息'}, component: () => import('./pages/shoucang/ShowShoucang.vue')},
            { path: 'yonghu', name: 'yonghu', meta: { title: '用户信息' }, component: () => import('./pages/shenfen/ShowShenFen.vue') },
            { path: 'gouwuche', name: 'gouwuche', meta: { title: '购物车信息' }, component: () => import('./pages/gouwuche/ShowGuwuche.vue') },
            { path: 'ddxq', name: 'ddxq', meta: { title: '订单详情' }, component: () => import('./pages/ddxiangqing/ShowDDXQ.vue') },
            { path: 'dizhi', name: 'dizhi', meta: { title: '地址信息' }, component: () => import('./pages/dizhi/ShowDizhi.vue')},
            { path: 'dingdan', name: 'dingdan', meta: { title: '订单信息' }, component: () => import('./pages/dingdan/ShowDingdan.vue')},
            { path: 'shangjia', name: 'shangjia', meta: { title: '商家信息' }, component: () => import('./pages/shangjia/ShowShangJia.vue') },
            { path: 'peisong', name: 'peisong', meta: { title: '配送信息' }, component: () => import('./pages/peisong/ShowPeiSong.vue') },
            { path: 'pingjia', name: 'pingjia', meta: { title: '评价信息' }, component: () => import('./pages/pingjia/ShowPingJia.vue') },
            { path: 'caipin', name: 'cpxx', meta: { title: '菜品信息' }, component: () => import('./pages/caipin/ShowCaiPin.vue')},
            { path: 'sjwork', name: 'sjwork', meta: { title: '商家营业状况' }, component: () => import('./components/SjWork.vue')},
            { path: 'showuser', name: 'showuser', meta: { title: '个人主页' }, component: () => import('./pages/user/ShowUser.vue')},
            { path: 'showdd', name: 'showdd', meta: { title: '我的订单' }, component: () => import('./pages/user/UserDingdan.vue')},
            { path: 'showxx', name: 'showxx', meta: { title: '我的消息' }, component: () => import('./pages/user/UserXiaoxi.vue')},
            { path: 'showsc', name: 'showsc', meta: { title: '我的收藏' }, component: () => import('./pages/user/UserShoucang.vue')},
            { path: 'cp', name: 'cp', meta: { title: '商家菜品' }, component: () => import('./pages/usersj/cp.vue')},
            { path: 'sjpj', name: 'sjpj', meta: { title: '商家评价' }, component: () => import('./pages/pingjia/sjpj.vue')},
            { path: 'sjdd', name: 'sjdd', meta: { title: '商家订单' }, component: () => import('./pages/usersj/sjdd.vue')},
            { path: 'qsdd', name: 'qsdd', meta: { title: '骑手订单' }, component: () => import('./pages/qswork/qsDingDan.vue')},
            { path: 'sjpj', name: 'sjpj', meta: { title: '商家评价' }, component: () => import('./pages/usersj/pingjiaSj.vue')},


            ]},
        /*测试页面*/
        { path: '/test', name: 'test', meta: { title: '测试' }, component: () => import('./pages/test/XiaDan.vue')},
    ],
})

// @ts-ignore
router.beforeEach((to, from, next) => {
    document.title = to.meta.title?.toString() || '饿了美'
    // 检查用户是否已登录
    const userInfo = JSON.parse(localStorage.getItem('userInfo')) || null;
    console.log(userInfo)
    // 登录验证逻辑
    if(to.path !== '/showshouye' && !userInfo) {
        ElMessage.error('请先登录')
        return next({ name: 'showshouye' })
    }

    return next()
})

export default router